﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using ColdStorage.DOL;
using System.Windows.Controls.Primitives;
using ColdStorage.Reference_Path;
using System.Xml;
using System.Reflection;

namespace ColdStorage.GUI
{
    /// <summary>
    /// Interaction logic for ucMainStorageCold.xaml
    /// </summary>
    public partial class ucMainStorageCold : UserControl
    {
        Room rRoom = new Room();
        public Cold_Storage cColdStorage = new Cold_Storage();

        int iRoom = 1;
        string key = "";

        TabItem tabitem = new TabItem();

        public delegate void GetRoomCold(Room room, bool update);
        public GetRoomCold myRoomCold;

        public delegate void DeleteRoom();
        public DeleteRoom DelRoom;

        public ucMainStorageCold()
        {
            InitializeComponent();

            ucRoomCold1.myRoomCold = new ucRoomCold.GetRoomCold(addRoom);
            ucRoomCold1.MyRoomArea = new ucRoomCold.GetRoomArea(GetRoomArea);
            ucRoomCold1.MyRoomCapacity = new ucRoomCold.GetRoomCapacity(getRoomCapacity);
            ucRoomCold1.DelRoom = new ucRoomCold.DeleteRoom(deleteRoom);
            ucRoomCold1.MyProudctId = new ucRoomCold.GetProductId(GetID);
            ucRoomCold1.myProductType = new ucRoomCold.GetProductType(GetProduct_type);
            ucRoomCold1.MyRoomTypeId = new ucRoomCold.GetRoomTypeID(GetRoomTypeId);

            ucProduct1.myProductInputTemp = new ucProduct.GetProductInputTemp(GetProductInputTemp);
            ucProduct1.myProducStorageTemp = new ucProduct.GetProductStorageTemp(GetProductStorageTemp);

            ucConstructionCold1.MyWalllist = new ucConstructionCold.GetWallList(getwall);
            ucConstructionCold1.MyRoofList = new ucConstructionCold.GetRoofList(getRoof);
            ucConstructionCold1.MyFloor = new ucConstructionCold.GetFloor(getFloor);
            ucConstructionCold1.DeleteMyWall = new ucConstructionCold.DeleteWall(deleteWall);
            ucConstructionCold1.DeleteMyRoof = new ucConstructionCold.DeleteRoof(deleteRoof);
        }

        #region TAB ROOM

        //Nhiệt độ cao nhất
        public void getMaxTemperature(string maxtemp)
        {
            ucRoomCold1.loadMaxTemp(maxtemp);
        }

        //Room Capacity
        public void getRoomCapacity(double roomcapacity)
        {
            ucProduct1.RoomCapacity = roomcapacity;
        }

        //Đối tượng Room
        public void addRoom(Room ro, bool update)
        {
            if (update)
            {
                if (cColdStorage.lRoom.IndexOf(ro) != -1)
                {
                    cColdStorage.lRoom[cColdStorage.lRoom.IndexOf(ro)] = ro;
                    myRoomCold(ro, true);
                }
            }
            else
            {
                ro.sRoomName = "Room " + iRoom;
                iRoom++;
                cColdStorage.AddRoom(ro);
                myRoomCold(ro, false);
            }
        }

        public void deleteRoom()
        {
            cColdStorage.DeleteRoom(rRoom);
            if (cColdStorage.lRoom.Count == 0)
            {
                EnableRoom(false);
                iRoom = 1;
            }
            DelRoom();
        }

        ///Room Type Id
        public void GetRoomTypeId(int roomtypeid)
        {
            ucInternalLoadCold1.RoomTypeId = roomtypeid;
            ucInternalLoadCold1.HeatDoorOpening();
        }

        //Room Area
        public void GetRoomArea(double roomArea)
        {
            ucInternalLoadCold1.RoomArea = roomArea;
            ucInternalLoadCold1.HeatDoorOpening();
        }

        public void loadroom(Room room)
        {
            rRoom = room;
            EnableRoom(true);
            ucRoomCold1.loadroom(room);
            ucProduct1.loadProduct(room.pro);
            ucInternalLoadCold1.loadInternal(room.itl);
            ucConstructionCold1.loadFloor(room.fFloor);
            ucConstructionCold1.loadRoof(room.listRoof);
            ucConstructionCold1.loadWall(room.listWall);
        }

        public void EnableRoom(bool b)
        {
            ucRoomCold1.EnableRoom(b);
            tabRoom.IsEnabled = b;
            tabProduct.IsEnabled = b;
            tabInternal.IsEnabled = b;
            tabConstruction.IsEnabled = b;
        }

        public void getColdStorageInfo(Cold_Storage cs)
        {
            cColdStorage = cs;
            EnableRoom(false);
            tabRoom.IsSelected = true;
        }
        #endregion

        #region TAB PRODUCT

        public void GetID(int productid)
        {
            ucProduct1.pId = productid;
            ucInternalLoadCold1.Product_id = productid;
            ucInternalLoadCold1.Respiration();
        }

        public void GetProduct_type(int producttype)
        {
            ucInternalLoadCold1.Product_type = producttype;
            ucInternalLoadCold1.Respiration();
        }

        public void GetProductInputTemp(int inputtemp)
        {
            ucInternalLoadCold1.ProductInputTemp = inputtemp;
            ucInternalLoadCold1.Respiration();
        }

        public void GetProductStorageTemp(int storagetemp)
        {
            ucInternalLoadCold1.ProductStorageTemp = storagetemp;
            ucInternalLoadCold1.Respiration();
        }
        #endregion

        #region TAB CONSTRUCTION
        //Đối tượng Floor
        public void getFloor(object f)
        {
            rRoom.fFloor = (Floor)f;
            rRoom.CalculateCladdingHeat();
            //MessageBox.Show(rRoom.fFloor.dbFloorLength.ToString());
        }

        // Xóa tường
        void deleteWall(object w)
        {
            rRoom.listWall.Remove((Wall)w);
        }

        //Xóa mái
        void deleteRoof(object r)
        {
            rRoom.listRoof.Remove((Roof)r);
        }

        //Đối tượng tường
        public void getwall()
        {
            rRoom.listWall.Clear();
            foreach (Wall w in ucConstructionCold1.lstWall.Items)
            {
                rRoom.listWall.Add(w);
            }
            foreach (Wall w in rRoom.listWall)
            {
                rRoom.CalculateCladdingHeat();
            }

        }

        //Đối tượng Roof
        public void getRoof()
        {
            rRoom.listRoof.Clear();
            foreach (Roof r in ucConstructionCold1.lstRoof.Items)
            {
                rRoom.listRoof.Add(r);
            }
            foreach (Roof r in rRoom.listRoof)
            {
                rRoom.CalculateCladdingHeat();
            }
        }
        #endregion

        #region PROCESS TAB CHANGE
        private void tabMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (tabMain.SelectedItem != null)
            {
                if (tabitem.Header != null)
                {
                    if (((TabItem)tabMain.SelectedItem).Header.ToString() != tabitem.Header.ToString())
                    {
                        if (tabitem.Header.ToString() == "Room")
                        {
                            ucRoomCold1.ApplyRoom();
                        }
                        else
                        {
                            if (tabitem.Header.ToString() == "Product")
                            {
                                ucProduct1.ApplyProduct();
                            }
                            else
                            {
                                if (tabitem.Header.ToString() == "Internal Load")
                                {
                                    ucInternalLoadCold1.AppInternal();
                                }
                                else
                                {
                                    ucConstructionCold1.AppConstruction();
                                }
                            }
                        }
                    }
                }
                tabitem = (TabItem)tabMain.SelectedItem;
            }
        }

        public void SaveTab()
        {
            if (cColdStorage.lRoom.Count != 0)
            {
                if (tabitem.Header.ToString() == "Room")
                {
                    ucRoomCold1.ApplyRoom();
                }
                else
                {
                    if (tabitem.Header.ToString() == "Product")
                    {
                        ucProduct1.ApplyProduct();
                    }
                    else
                    {
                        if (tabitem.Header.ToString() == "Internal Load")
                        {
                            ucInternalLoadCold1.AppInternal();
                        }
                        else
                        {
                            ucConstructionCold1.AppConstruction();
                        }
                    }
                }
            }
        }
        #endregion        

        private void grid1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.F1)
            {
                switch (tabitem.Header.ToString())
                {
                    case "Room":
                        System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, RoomScreenReference.ResourceManager.GetString(ucRoomCold1.key));
                        break;
                    case "Product":
                        System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, ProductScreenReference.ResourceManager.GetString(ucProduct1.key));
                        break;
                    case "Internal Load":
                        System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, InternalLoadScreenReference.ResourceManager.GetString(ucInternalLoadCold1.key));
                        break;
                    case "Construction":
                        System.Windows.Forms.Help.ShowHelp(null, ReferencePath.ReferenceFile, System.Windows.Forms.HelpNavigator.KeywordIndex, ConstructionScreenReference.ResourceManager.GetString(ucConstructionCold1.key));
                        break;
                }                
            }
        }

        public void LoadObject(Object obj) {
            switch (obj.GetType().Name)
            {
                case "Product":
                    rRoom.pro = (Product)obj;
                    break;
                case "InternalLoad":
                    rRoom.itl = (InternalLoad)obj;
                    break;
                case "Floor":
                    rRoom.fFloor = (Floor)obj;
                    break;
                case "Wall":
                    rRoom.listWall.Add((Wall)obj);
                    break;
                case "Roof":
                    rRoom.listRoof.Add((Roof)obj);
                    break;
            }
        }

        public void SaveProject(XmlTextWriter tw)
        {
            tw.WriteStartElement("ColdStorage");
            foreach (PropertyInfo property in cColdStorage.GetType().GetProperties())
            {
                tw.WriteAttributeString(property.Name, property.GetValue(cColdStorage, null).ToString());
            }
            foreach (Room r in cColdStorage.lRoom)
            {
                tw.WriteStartElement("Room");
                foreach (PropertyInfo property in r.GetType().GetProperties())
                {
                    tw.WriteAttributeString(property.Name, property.GetValue(r, null).ToString());
                }
                tw.WriteStartElement("Product");
                foreach (PropertyInfo property in r.pro.GetType().GetProperties())
                {
                    tw.WriteAttributeString(property.Name, property.GetValue(r.pro, null).ToString());
                }
                tw.WriteEndElement();
                tw.WriteStartElement("InternalLoad");
                foreach (PropertyInfo property in r.itl.GetType().GetProperties())
                {
                    tw.WriteAttributeString(property.Name, property.GetValue(r.itl, null).ToString());
                }
                tw.WriteEndElement();
                tw.WriteStartElement("Construction");
                tw.WriteStartElement("Floor");
                foreach (PropertyInfo property in r.fFloor.GetType().GetProperties())
                {
                    tw.WriteAttributeString(property.Name, property.GetValue(r.fFloor, null).ToString());
                }
                tw.WriteEndElement();
                foreach (ColdStorage.DOL.Wall wall in r.listWall)
                {
                    tw.WriteStartElement("Wall");
                    foreach (PropertyInfo property in wall.GetType().GetProperties())
                    {
                        tw.WriteAttributeString(property.Name, property.GetValue(wall, null).ToString());
                    }
                    tw.WriteEndElement();
                }
                foreach (ColdStorage.DOL.Roof roof in r.listRoof)
                {
                    tw.WriteStartElement("Roof");
                    foreach (PropertyInfo property in roof.GetType().GetProperties())
                    {
                        tw.WriteAttributeString(property.Name, property.GetValue(roof, null).ToString());
                    }
                    tw.WriteEndElement();
                }
                tw.WriteEndElement();
                tw.WriteEndElement();
            }
            tw.WriteEndElement();
        }

        public object GetResult() {
            return cColdStorage;
        }
    }
}
